package com.hd.trans.network;

import androidx.annotation.NonNull;

import com.hd.trans.TranslationInterior;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class HeaderInterceptor implements Interceptor {
    private boolean isNormalMode = true;
    private boolean isSSE = false;

    public HeaderInterceptor(boolean isNormalMode) {
        this.isNormalMode = isNormalMode;
    }

    public HeaderInterceptor(boolean isNormalMode, boolean isSSE) {
        this.isNormalMode = isNormalMode;
        this.isSSE = isSSE;
    }

    @NonNull
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        Request originalRequest = chain.request();
        Request.Builder requestBuilder = originalRequest.newBuilder()
                .addHeader("X-Product", TranslationInterior.getProductId())
                .addHeader("X-Domain", originalRequest.url().host())
                .addHeader("X-Channel", TranslationInterior.getChannel())
                .addHeader("X-Credits", TranslationInterior.getCredits())
                .addHeader("X-Version", TranslationInterior.getAppVersionName());

        if(!isNormalMode){
            //Accept: text/event-stream
            requestBuilder.addHeader("Accept", isSSE?"text/event-stream":"application/json")
                    .addHeader("Content-Type", "application/json; charset=utf-8")
                    .addHeader("Charset", "UTF-8")
                    .addHeader("Connection", "Keep-Alive");
        }
        requestBuilder.method(originalRequest.method(), originalRequest.body());
        Request request = requestBuilder.build();

        return chain.proceed(request);

//                Request request = chain.request();
//                if (!isNetworkReachable(AudioCovertApplication.getInstance().getApplicationContext())) {
//                    Toast.makeText(AudioCovertApplication.getInstance().getApplicationContext(), "暂无网络", Toast.LENGTH_SHORT).show();
//                    request = request.newBuilder()
//                            .cacheControl(CacheControl.FORCE_CACHE)//无网络时只从缓存中读取
//                            .build();
//                }
//
//                Response response = chain.proceed(request);
//                if (isNetworkReachable(AudioCovertApplication.getInstance().getApplicationContext())) {
//                    int maxAge = 60 * 60; // 有网络时 设置缓存超时时间1个小时
//                    response.newBuilder()
//                            .removeHeader("Pragma")
//                            .header("Cache-Control", "public, max-age=" + maxAge)
//                            .build();
//                } else {
//                    int maxStale = 60 * 60 * 24 * 28; // 无网络时，设置超时为4周
//                    response.newBuilder()
//                            .removeHeader("Pragma")
//                            .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
//                            .build();
//                }
//                return response;//chain.proceed(request);
    }
}
